﻿using System;
using System.Collections.Generic;
using System.Linq;
using CarTrackr.Business;
using CarTrackr.Data;


namespace CarTrackr.Tests.Repository
{
    public class FakeRepository : IRepository
    {
        private DataStore dataStore;

        public FakeRepository()
        {
            dataStore = new DataStore();
        }

        public List<T> ListUsers<T>()
        {
            
            List<T> users = new List<T>();

            foreach (User user in  dataStore.Users)
            {

                users.Add(Converter.ToBusinessBase<T>(user));
            }

            return users;
        }

     


        public T GetUserById<T>(Guid id)
        {
            User found = dataStore.Users.Where(user => user.UserId == id).FirstOrDefault();

            return Converter.ToBusinessBase<T>(found);
        }


        public T GetUserByName<T>(string userName)
        {
            User found = dataStore.Users.Where(user => user.UserName == userName).FirstOrDefault();

            return Converter.ToBusinessBase<T>(found);
        }

        public List<T> ListCars<T>(Guid userId)
        {
            User found = dataStore.Users.Where(user => user.UserId == userId).FirstOrDefault();

            List<T> cars = new List<T>();

            foreach (Car car in found.Cars)
            {
                cars.Add(Converter.ToBusinessBase<T>(car));
            }

            return cars;

        }


        public T GetCarById<T>(Guid id)
        {

            Car found = dataStore.Cars.Where(car => car.Id == id).FirstOrDefault();

            return Converter.ToBusinessBase<T>(found);

        }


        public T GetCarByLicensePlate<T>(string licensePlate)
        {
            Car found = dataStore.Cars.Where(car => car.LicensePlate == licensePlate).FirstOrDefault();

            return Converter.ToBusinessBase<T>(found);
        }


        public void AddCar<T>(T car, Guid userId)
        {

            Car carToAdd = Converter.ToBusinessBase<Car>(car);

            User found = dataStore.Users.Where(user => user.UserId == userId).FirstOrDefault();

            found.AddCar(carToAdd);

            dataStore.Cars.Add(carToAdd);
        }

        public void UpdateCar<T>(T car)
        {
            Car carToUpdate = Converter.ToBusinessBase<Car>(car);

            User user = FindUserWhoOwnsTheCar(carToUpdate.Id);

            Car original1 = user.Cars.Where(c => c.Id == carToUpdate.Id).FirstOrDefault();

            original1 = carToUpdate;

            Car origina2 = dataStore.Cars.Where(c => c.Id == carToUpdate.Id).FirstOrDefault();

            origina2 = carToUpdate;


        }

        public void RemoveCar(Guid id)
        {

            Car original = dataStore.Cars.Where(c => c.Id == id).FirstOrDefault();

            foreach(Refuelling refuelling in original.Refuellings)
            {
                Refuelling found = dataStore.Refuellings.Where(r => r.Id == refuelling.Id).FirstOrDefault();

                dataStore.Refuellings.Remove(found);
 
            }

            User user = FindUserWhoOwnsTheCar(id);

            user.Cars.Remove(original);

            dataStore.Cars.Remove(original);

        }



        public User FindUserWhoOwnsTheCar(Guid carId)
        {
            foreach (User user in dataStore.Users)
            {
                foreach (Car car in user.Cars)
                {
                    if (car.Id == carId)
                    {
                        return user;
                    }
                }
            }

            return null;
        }


        public List<T> ListRefuellings<T>(Guid id)
        {
            Car found = dataStore.Cars.Where(car => car.Id == id).FirstOrDefault();

            List<T> refuellings = new List<T>();

            foreach (Refuelling refuelling in found.Refuellings)
            {
                refuellings.Add(Converter.ToBusinessBase<T>(refuelling));
            }

            return refuellings;
        }


        public void AddRefuelling<T>(T refuelling, Guid carId)
        {

            Refuelling refuellingToAdd = Converter.ToBusinessBase<Refuelling>(refuelling);

            Car found = dataStore.Cars.Where(c => c.Id == carId).FirstOrDefault();

            found.AddRefuelling(refuellingToAdd);

            dataStore.Refuellings.Add(refuellingToAdd);

        }

        public void RemoveRefuelling(Guid id)
        {
            Car car = FindCarthatContainsRefuelling(id);

            Refuelling original1 = car.Refuellings.Where(r => r.Id == id).FirstOrDefault();

            Refuelling original2 = dataStore.Refuellings.Where(r => r.Id == id).FirstOrDefault();

            car.Refuellings.Remove(original1);

            dataStore.Refuellings.Remove(original2);

        }


        private Car FindCarthatContainsRefuelling(Guid id)
        {
            foreach (Car car in dataStore.Cars)
            {
                foreach (Refuelling refuelling in car.Refuellings)
                {
                    if (refuelling.Id == id)
                    {
                        return car;
                    }
                }
            }

            return null;
        }


        public User GetCurrentUser()
        {
            return dataStore.Users[0];
        }

    }


}